//
//  Base.swift
//  CocoaLumberjack
//
//  Created by mac on 2022/6/15.
//

import Foundation

struct UGImageError:Error{
    
    var code:Int = 0
    var msg:String?
    var debugmsg:String?
    
    var debugDescription:String{
        let msg:String = debugmsg ?? msg ?? "未知错误"
        
        return "code:\(code) message:\(msg)"
    }
    var localizedDescription:String{
        return msg ?? "错误"
    }
}

// MARK: log
enum UGImageLogLevel:Int{
    case info = 0
    case debug
    case warning
    case error
}

internal class LogManage{
    static var logLevel:UGImageLogLevel = .info
    static var isdesc = false
}

func log(_ items: String..., level:UGImageLogLevel = .info, filename: String = #file, function : String = #function, line: Int = #line, separator: String = "\n", terminator: String = "\n") {
  
#if DEBUG
    if level.rawValue<LogManage.logLevel.rawValue{
        return
    }
    var levelstr = ""
    switch level{
    case .info: levelstr = "🕷 info"
    case .debug: levelstr = "🐜 debug"
    case .warning:  levelstr = "🦜 warning"
    case .error: levelstr = "🦞 error"
    }
    var log =  "\(levelstr): \(items.map { "\($0)" }.joined(separator: separator))"
    if LogManage.isdesc{
        log = """
             \(levelstr)
            file:   \(URL(fileURLWithPath: filename).lastPathComponent)
            line:   \(line)
            func:   \(function)
            msg:   \(items.map { "\($0)" }.joined(separator: separator))
            
            """
    }
    
    Swift.print(log, terminator: terminator)
#endif
}

// MARK: Doucment

internal extension UIApplication{

  
    /// Documnets目录
    static let KDocumentPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
                                                                    FileManager.SearchPathDomainMask.userDomainMask, true)
    static let KDocumnetPath = KDocumentPaths[0]
    
    /// Cache目录
    static let KCachePaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory,
                                                                 FileManager.SearchPathDomainMask.userDomainMask, true)
    static let KCachePath = KCachePaths[0]
}
